package com.soilflow.web.controller.soilflow;

import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.page.TableDataInfo;
import com.soilflow.common.domain.LandNews;
import com.soilflow.common.domain.dto.LandNewsDto;
import com.soilflow.common.domain.dto.LandNoticeDto;
import com.soilflow.common.domain.vo.LandNewsVo;
import com.soilflow.common.domain.vo.LandNoticeVo;
import com.soilflow.info.service.LandNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新闻信息
 */
@RestController
@RequestMapping("/land/news")
public class LandNewsController extends BaseController {

    @Autowired
    private LandNewsService landNewsService;


    /**
     * 新闻列表
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo landNewsList(LandNewsDto dto)
    {
        startPage();
        List<LandNewsVo> list = landNewsService.landNewsList(dto);
        return getDataTable(list);
    }

    @GetMapping("/landList")
    public TableDataInfo landList(LandNewsDto dto)
    {
        startPage();
        List<LandNewsVo> list = landNewsService.landList(dto);
        return getDataTable(list);
    }


    /**
     * 添加新闻信息
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public AjaxResult addNewNotice(@RequestBody LandNewsDto dto){
        dto.setDeptId(getDeptId());
        Boolean result = landNewsService.addNewNotice(dto);
        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error("请勿重复添加");
    }

    /**
     * 删除新闻
     * @param ids
     * @return
     */
    @DeleteMapping("/del/{ids}")
    public AjaxResult delLandNews(@PathVariable List<Long> ids){
        landNewsService.removeByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 更新新闻
     * @param dto
     * @return
     */
    @PutMapping("/update")
    public AjaxResult updateLandNews(@RequestBody LandNewsDto dto){
        dto.setDeptId(getDeptId());
        landNewsService.updateLandNews(dto);
        return AjaxResult.success();
    }

    /**
     * 回显详情
     * @return
     */
    @GetMapping("/detail/{id}")
    public AjaxResult getLandNewsById(@PathVariable Long id){
        LandNewsVo vo = landNewsService.getLandNewsById(id);
        return AjaxResult.success(vo);
    }
}
